package com.controller;
import com.entity.Option;
import com.entity.Subject;
import com.entity.UserInfo;
import com.entity.vo.SubjectVo;
import com.github.pagehelper.PageInfo;
import com.service.OptionService;
import com.service.SubjectService;
import com.service.UserService;
import com.util.JackUtil;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
@Controller
public class SubjectController {
    @Resource
    private SubjectService subjectService;
    @Resource
    private OptionService optionService;
	//进入查看结果方法
    @RequestMapping("/selectSubjectResultDetail")
    public String selectSubjectResultDetail(Integer subid,Model model) {
    	//根据id查询subject详细
    	SubjectVo svo =  this.subjectService.selectOneSubjectDetail(subid);
    	//投票结果
    	List<Map> lists =  this.subjectService.selectSubjectOptionBl(subid);
    	model.addAttribute("onevo", svo );
    	model.addAttribute("rs", lists );
		System.out.println(JackUtil.objecToString(lists));
    	return "result";
    }
    //后台标题列表查询
    @RequestMapping("/adminselectsubjectDetail")
    public String adminselectsubjectDetail(@RequestParam(defaultValue = "1") Integer index, @RequestParam(defaultValue = "5") Integer size, Model m) {
    	PageInfo<SubjectVo> info =  this.subjectService.selectSubjectDetailAdmin(index, size);
		System.out.println(JackUtil.objecToString(info));
    	m.addAttribute("pinfo", info);
    	return "admin/subject";
    }
    //发布投票
    @RequestMapping("/adminsaveSubject")
    public String createSubject(Subject sub, String[] op,HttpSession session) {
    	Integer temp =  this.subjectService.saveSubject(sub, op);
    	if(temp == 1) {//发布投票成功
    		session.setAttribute("saveok", 1);
    		return "admin/savesubok";
    	}else {
    		//发布失败 标题不能重复
    		session.setAttribute("saveerr", 1);
    		session.setAttribute("sub", sub);
    		session.setAttribute("op", op);
    		return "admin/subinfo";
    	}
    }
    //根据vsid删除 subject
    @RequestMapping("/deleteSubjectByid")
    public String deleteSubjectByid(Integer vsid,HttpSession session) {
    	Integer temp =  this.subjectService.deleteSubjectByid(vsid);
    	if(temp == 2) { //删除失败 投过票 不能删除
    		session.setAttribute("delsuberr", "1");
    	}else {
    		//删除成功
    		session.setAttribute("delsubok", "1");
    	}
    	return "redirect:/adminselectsubjectDetail";
    }
    //查询subject详细 用于修改前 显示
    @RequestMapping("/selectSubjectAndOptionInfo")
    public String editOption(Integer vsid,Model m) {
    	Subject sub = this.subjectService.selectOneSubject(vsid);
    	List<Option> op = this.optionService.selectOptionById(vsid);
    	m.addAttribute("sub", sub);
    	m.addAttribute("op", op);
    	return "/admin/editsubject";
    }
}
